using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Ledger.Merchconf;
using JlPay.Sdk.Models.Ledger.Merchconf;

namespace JlPay.Sdk.Examples.Ledger.Merchconf.MerchIssueOpen
{
    /// <summary>
    /// 商户分账发行开通独立示例程序
    /// </summary>
    public class MerchIssueOpenDemo
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args">命令行参数</param>
        /// <returns>任务</returns>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("==========================================");
            Console.WriteLine("===== 商户分账发行开通独立示例程序 =====");
            Console.WriteLine("==========================================");
            Console.WriteLine();

            await MerchIssueOpenDemoAsync();

            Console.WriteLine("==========================================");
            Console.WriteLine("===== 示例运行完毕 =====");
            Console.WriteLine("==========================================");
        }

        /// <summary>
        /// 创建配置对象
        /// </summary>
        /// <returns>配置对象</returns>
        private static Config CreateConfig()
        {
            return DemoConfig.CreateConfigWithCrypto();
        }

        /// <summary>
        /// 构建商户分账发行开通请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static MerchIssueOpenRequest BuildMerchIssueOpenRequest()
        {
            var request = new MerchIssueOpenRequest();
            
            // 设置请求参数
            request.OrgCode = "50720711";
            request.MchId = "849581183510017";
            request.LedgerModule = "02";
            request.MaxSplitRate = "20";
            request.SourceIds = new List<string> 
            { 
                "jlpay/2024-08-30/e22bc590f433418483626fded04f3362.png", 
                "jlpay/2024-08-30/e22bc590f433418483626fded04f3363.png" 
            };
            request.FeeRate = "2";
            request.Pbthf = "0";
            request.LowAmt = "1";
            request.UpAmt = "10";
            request.NotifyUrl = "https://api.mch.weixin.qq.com/sns/notify";
            
            return request;
        }

        /// <summary>
        /// 商户分账发行开通示例
        /// </summary>
        public static async Task MerchIssueOpenDemoAsync()
        {
            Console.WriteLine("开始执行商户分账发行开通示例");
            
            try
            {
                // 创建配置对象和商户配置服务
                var config = CreateConfig();
                var merchconfService = new MerchconfService(config);
                
                // 创建商户分账发行开通请求
                var request = BuildMerchIssueOpenRequest();
                
                // 发送请求
                var response = await merchconfService.OpenMerchIssueAsync(request);
                
                // 处理响应结果
                response.HandleResponse("商户分账发行开通", config);
            }
            catch (Exception ex)
            {
                var config = CreateConfig();
                var response = new MerchIssueOpenResponse();
                response.HandleResponseWithException("商户分账发行开通", config, ex);
            }
            
            Console.WriteLine("商户分账发行开通示例执行完毕");
        }
    }
} 